package com.geek.water.controller;

import com.geek.water.constant.CodeConstant;
import com.geek.water.constant.MessageConstant;
import com.geek.water.entity.Area;
import com.geek.water.exception.WaterException;
import com.geek.water.result.Result;
import com.geek.water.service.AreaService;
import com.geek.water.vo.AreaTreeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/7/28
 */
@RestController
@RequestMapping("/admin/area")
@RequiredArgsConstructor
@Api(tags = "后台系统-区域管理")
public class AreaController {

    private final AreaService areaService;

    @ApiOperation("根据ID获取区域信息")
    @PreAuthorize("hasAuthority('area:view')")
    @GetMapping("/{id}")
    public Result getAreaById(@PathVariable Long id) {
        try {
            Area area = areaService.getById(id);
            if (area != null) {
                return new Result(CodeConstant.SUCCESS, "查询成功", area);
            }
            return new Result(CodeConstant.FAIL, "区域不存在");
        } catch (Exception e) {
            return new Result(CodeConstant.FAIL, "查询失败");
        }
    }

    @ApiOperation("新增区域")
    @PreAuthorize("hasAuthority('area:operate')")
    @PostMapping
    public Result createArea( @RequestBody Area area) {
        areaService.validateArea(area);
        boolean saved = areaService.save(area);
        if (saved) {
            return new Result(CodeConstant.SUCCESS, MessageConstant.ADD_AREA_SUCCESS);
        }
        return new Result(CodeConstant.FAIL, MessageConstant.ADD_AREA_FAIL);
    }

    @ApiOperation("更新区域信息")
    @PreAuthorize("hasAuthority('area:operate')")
    @PutMapping
    public Result updateArea( @RequestBody Area area) {
        areaService.validateArea(area);
        boolean updated = areaService.updateById(area);
        if (updated) {
            return new Result(CodeConstant.SUCCESS, MessageConstant.UPDATE_AREA_SUCCESS);
        }
        return new Result(CodeConstant.FAIL, MessageConstant.UPDATE_AREA_FAIL);
    }

    @ApiOperation("删除区域")
    @PreAuthorize("hasAuthority('area:operate')")
    @DeleteMapping("/{id}")
    public Result deleteArea(@PathVariable Long id) {
        try {
            areaService.validateAreaBeforeDelete(id);
            boolean removed = areaService.removeById(id);
            return removed ?
                    new Result(CodeConstant.SUCCESS, MessageConstant.DELETE_AREA_SUCCESS) :
                    new Result(CodeConstant.FAIL, MessageConstant.DELETE_AREA_FAIL);
        } catch (WaterException e) {
            return new Result(CodeConstant.FAIL, e.getMessage());
        }
    }



    @ApiOperation("获取站点的区域树")
    @PreAuthorize("hasAuthority('area:view')")
    @GetMapping("/{siteId}/areas")
    public Result getAreaTree(@PathVariable Long siteId) {
        List<Area> areas = areaService.lambdaQuery()
                .eq(Area::getSiteId, siteId)
                .list();

        List<AreaTreeVO> tree = areaService.buildAreaTree(areas);
        // 即使没有区域数据，也返回成功状态和空数组
        return new Result(CodeConstant.SUCCESS, MessageConstant.QUERY_AREA_SUCCESS, tree);
    }

    @ApiOperation("获取所有区域树形列表")
    @PreAuthorize("hasAuthority('area:view')")
    @GetMapping("/tree")
    public Result getAllAreaTree() {
        List<Area> areas = areaService.list();
        List<AreaTreeVO> areaTree = areaService.buildAreaTree(areas);
        return new Result(CodeConstant.SUCCESS, "查询成功", areaTree);
    }
}